//解决键盘顶起
$(".text div").blur(function () {
    $("html,body").animate({scrollTop: document.documentElement.clientHeight}, 500);
});



//避免键盘遮挡
var textDiv = $('.text div');//the element I want to monitor

textDiv.bind('DOMNodeInserted', function(e) {

    
     setTimeout(function(){
        document.body.scrollTop = document.body.scrollHeight;
    },300);

});




$(".text div").click(function(){
    document.body.scrollTop = document.body.scrollHeight;
})

//回车换行发送消息
document.addEventListener('keydown', function (event) {
    if (event.key == 'Enter') {
        let message = $(".text div").text();
        if (message.length == 0) {
            return false;
        }
        let user_name=$("input[name='user_name']").val();
        let client_id=$("input[name='client_id']").val();
        let room_id=$("input[name='room_id']").val();
        let sex=Number($("input[name='sex']").val());
        let address=$(".address").val();

        let send = {
            message: message,
            room_id: room_id,
            client_id: client_id,
            sex:sex,
            address:address,
            user_name:user_name
        }
        $.ajax({
            type: "post",
            url: "/send",
            data: send,
            success: function (data) {
                $(".text div").text('');
            }
        })
    }

})

//发送键点击的事件
$(".send").click(function () {
     message=$(".text div").text();

    if (message.length == 0) {
        return false;
    }

    let address=$(".address").val();
    let send = {
        message: message,
        room_id: room_id,
        client_id: client_id,
        sex: sex,
        address:address,
        user_name:user_name
    }
    $.ajax({
        type: "post",
        url: "/send",
        data: send,
        success: function (data) {
            $(".text div").text('');
        }
    })
})


//表情包事件
$(".layui-icon-face-smile-b").click(function () {
    layer.msg("表情,暂时只能发软键盘中自带的emoji表情哦~")
})
